#!usr/bin/env/ python
import sys

from banner_class import Banner

def _create_dataset ( start_point, lines, rows, tot_rows ):
        res = {}

        count = 0
        for v in rows:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1

        res [ 'rows' ] = tot_rows
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = lines

        return res

def add_tipo ( liwe, id_tipo = 0, name = '', w = 0, h = 0, playtime = 0, effect = 0 ):
	"""
	:INPUT:
		- id_tipo:	SAFE_NUMBER
		- name:		SAFE_DB_STRING
		- w:		SAFE_NUMBER
		- h:		SAFE_NUMBER
		- playtime:	SAFE_NUMBER
		- effect:	SAFE_NUMBER
		
	:PERMS:
		- admin
	"""
	b = Banner ( liwe )
	res = b.add_tipo ( id_tipo = id_tipo, name = name, w = w, h = h, playtime = playtime, effect = effect )
	return { 'id_tipo': res }

def del_tipo ( liwe, ids ):
        """
        :INPUT:
                - ids:              SAFE_STRING

        :PERMS:
                - admin:          
        """

        n = Banner ( liwe )
        n.del_tipo ( ids )

        return { "status": 'ok' }



def add_gruppo ( liwe, id_gruppo = 0, name = '' ):
	"""
	:INPUT:
		- id_gruppo:SAFE_NUMBER
		- name:		SAFE_DB_STRING
	:PERMS:
		- admin
	"""
	b = Banner ( liwe )
	res = b.add_gruppo ( id_gruppo = id_gruppo, name = name )
	return { 'id_gruppo': res }


def del_gruppo ( liwe, ids ):
        """
        :INPUT:
                - ids:              SAFE_STRING

        :PERMS:
                - admin:          
        """

        n = Banner ( liwe )
        n.del_gruppo ( ids )

        return { "status": 'ok' }

def add_banner ( liwe, id_banner = 0, id_gruppo = 0, id_tipo = 0, descr = '', data_on = '', data_off = '', peso = 1, lnk = '', tags = '', date = '' ):
	"""
	:INPUT:
		- id_banner:SAFE_NUMBER
		- id_gruppo:SAFE_NUMBER
		- id_tipo:	SAFE_NUMBER
		- descr:	SAFE_DB_STRING
		- data_on:	SAFE_DATE
		- data_off:	SAFE_DATE
		- peso:		SAFE_NUMBER
		- lnk:		SAFE_DB_STRING
		- tags:     SAFE_DB_STRING
		- date:     SAFE_DB_STRING
	:PERMS:
		- admin
	"""
	b = Banner ( liwe )

        try:
                bn_file = liwe.pyhp.uploads [ liwe.pyhp.uploads.keys () [ 0 ] ]
        except:
                bn_file = None

	#sys.stderr.write ( "DATE: %s - %s\n" % ( data_on, data_off ) )

        if not id_banner and not bn_file:
                # gestione errore
                liwe.err_code = 1
                liwe.err_descr = "Manca il file"
                return {}

	res = b.add_banner ( id_banner = id_banner, id_gruppo = id_gruppo, id_tipo = id_tipo, descr = descr, data_on = data_on, data_off = data_off, peso = peso, lnk = lnk, file_obj = bn_file, tags = tags, date = date)

	return { 'id_banner' : res }


def del_banner ( liwe, ids ):
        """
        :INPUT:
                - ids:              SAFE_STRING

        :PERMS:
                - admin:          
        """

        n = Banner ( liwe )
        n.del_banner ( ids )

        return { "status": 'ok' }

